export function calcMedia(data) {
  data.sort()
  const len = data.length
  if (data.length % 2 === 0) {
    return ((data[len / 2] + data[len / 2 - 1]) / 2).toFixed(2)
  }
  return (data[Math.floor(len / 2)]).toFixed(2)
}

export function calcMean(data) {
  let sum = 0; const num = data.length
  data.forEach(element => {
    sum += element
  })
  return (sum / num).toFixed(2)
}

export function calcStd(data, mean) {
  let sum = 0; const num = data.length
  data.forEach(element => {
    sum += Math.pow((element - mean), 2)
  })
  return Math.sqrt(sum / num).toFixed(2)
}

export function calcQuantile(data, quantile) {
  data.sort()
  const len = data.length; const tmp = (len + 1) * quantile
  if (tmp % 100 === 0) {
    return data[tmp / 100 - 1]
  }
  const idx1 = Math.ceil(tmp / 100) - 1; const idx2 = Math.floor(tmp / 100) - 1
  return ((data[idx1] + data[idx2]) / 2).toFixed(2)
}
